package com.polaris.live.widget

import android.content.Context
import android.graphics.drawable.BitmapDrawable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.RelativeLayout
import androidx.annotation.IntDef
import androidx.core.content.ContextCompat
import com.polaris.live.common.ext.autoDp
import com.polaris.live.databinding.WidgetLevelBinding
import com.polaris.live.utils.RichCharmImgUtil

/**
 * 等级控件
 *
 * @author Created by lucas on 2023/10/17 10:41
 */
class LevelView(context: Context, attributeSet: AttributeSet) :
    RelativeLayout(context, attributeSet) {

    private val mBinding = WidgetLevelBinding.inflate(LayoutInflater.from(context), this)

    /**
     * 设置豪等级
     */
    fun setRichsLevel(richs: Long, @LevelType type: Int = LEVEL_VIEW_NORMAL) {
        setLevel(RichCharmImgUtil.getCommonRichImg(richs), type)
    }

    /**
     * 设置魅等级
     */
    fun setCharmsLevel(charms: Long, @LevelType type: Int = LEVEL_VIEW_NORMAL) {
        setLevel(RichCharmImgUtil.getCommonCharmImg(charms), type)
    }

    /**
     * 设置以算好的壕等级
     */
    fun setNormalRichsLevel(richs: Int, @LevelType type: Int = LEVEL_VIEW_NORMAL) {
        setLevel(RichCharmImgUtil.getCommonRichImg(richs), type)
    }

    /**
     * 设置以算好的魅等级
     */
    fun setNormalCharmLevel(charms: Int, @LevelType type: Int = LEVEL_VIEW_NORMAL) {
        setLevel(RichCharmImgUtil.getCommonCharmImg(charms), type)
    }

    /**
     * 设置根据性别设置豪魅等级
     */
    fun setRichCharmBySexLevel(
        sex: Int,
        rich: Long? = null,
        charm: Long? = null,
        @LevelType type: Int = LEVEL_VIEW_NORMAL,
    ) {
        setLevel(RichCharmImgUtil.getRichCharmInt(sex, rich, charm), type)
    }

    /**
     * 设置等级
     */
    private fun setLevel(pair: Pair<Int, Int?>, @LevelType type: Int) {
        setLevel(pair.first, pair.second, type)
    }

    /**
     * 设置等级
     */
    private fun setLevel(level: Int, imgRes: Int?, @LevelType type: Int) {
        if (imgRes == null) {
            visibility = GONE
        } else {
            visibility = VISIBLE

            val newHeight = when (type) {
                LEVEL_VIEW_SUPER_BIG -> 20.autoDp
                LEVEL_VIEW_BIG -> 18.autoDp
                LEVEL_VIEW_NORMAL -> 16.autoDp
                LEVEL_VIEW_SMALL -> 12.autoDp
                LEVEL_VIEW_SUPER_SMALL -> 9.autoDp
                else -> 16.autoDp
            }

            val drawable = ContextCompat.getDrawable(context, imgRes)
            if (drawable is BitmapDrawable) {
                val bitmap = drawable.bitmap
                val dw = bitmap.width
                val dh = bitmap.height
                val lp = mBinding.bg.layoutParams
                lp.height = newHeight
                lp.width = ((dw / dh.toFloat()) * newHeight).toInt()
                mBinding.bg.layoutParams = lp
                val rootlp = mBinding.root.layoutParams
                rootlp.height = newHeight
                rootlp.width = ((dw / dh.toFloat()) * newHeight).toInt()
                mBinding.root.layoutParams = rootlp
            }
            val newTextSize = when (type) {
                LEVEL_VIEW_SUPER_BIG -> 12f
                LEVEL_VIEW_BIG -> 10f
                LEVEL_VIEW_NORMAL -> 9f
                LEVEL_VIEW_SMALL -> 7f
                LEVEL_VIEW_SUPER_SMALL -> 5f
                else -> 11f
            }
            mBinding.level.textSize = newTextSize
            mBinding.level.text = "Lv$level"
            mBinding.bg.setImageResource(imgRes)
        }
    }

    companion object {

        const val LEVEL_VIEW_SUPER_BIG = 0      //超大 20
        const val LEVEL_VIEW_BIG = 1            //大的 18
        const val LEVEL_VIEW_NORMAL = 2         //正常 16
        const val LEVEL_VIEW_SMALL = 3          //小的 12
        const val LEVEL_VIEW_SUPER_SMALL = 9    //超小 9

        @IntDef(
            LEVEL_VIEW_SUPER_BIG,
            LEVEL_VIEW_BIG,
            LEVEL_VIEW_NORMAL,
            LEVEL_VIEW_SMALL,
            LEVEL_VIEW_SUPER_SMALL
        )
        annotation class LevelType
    }

}